package java44th.group.service.admin.role;

import com.github.pagehelper.PageHelper;
import java44th.group.bean.admin.bo.role.CreateBo;
import java44th.group.bean.admin.bo.role.PermissionsBo;
import java44th.group.bean.json.ChildrenDTOX;
import java44th.group.bean.json.DataDTO;
import java44th.group.bean.json.SystemPermissionsDTO;
import java44th.group.bean.po.MarketPermission;
import java44th.group.bean.po.MarketRegion;
import java44th.group.bean.po.MarketRole;
import java44th.group.bean.admin.vo.role.*;
import java44th.group.mapper.MarketRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.LinkedList;

/**
 * @Aurhor: 王可龙
 * @DATE: 2022/10/12 20:24
 * @Package:java44th.group.service.admin.role
 * @Description:
 * @Version 1.0
 */
@Service
public class MarketRoleServiceImpl implements MarketRoleService {


    @Autowired
    MarketRoleMapper marketRoleMapper;


    @Override
    public RoleDateVo list(Integer page, Integer limit, String name,String sort, String order) {
        PageHelper.startPage(page, limit);
        LinkedList<MarketRole> marketRoles = marketRoleMapper.selectAllRoleData(name);
        LinkedList<RoleListVo> roleListVos = new LinkedList<>();
        for (MarketRole marketRole : marketRoles) {
            RoleListVo roleListVo = new RoleListVo();
            roleListVo.setId(marketRole.getId());
            roleListVo.setName(marketRole.getName());
            roleListVo.setDesc(marketRole.getDesc());
            roleListVo.setEnabled(marketRole.getEnabled());
            roleListVo.setAddTime(marketRole.getAddTime().toString());
            roleListVo.setUpdateTime(marketRole.getUpdateTime().toString());
            roleListVo.setDeleted(marketRole.getDeleted());
            roleListVos.add(roleListVo);
        }
        Integer count=0;
        if (name!=null){
            count=marketRoleMapper.countByName(name);
        }else {
            count = marketRoleMapper.countByExample(null);
        }
        RoleDateVo roleDateVo = new RoleDateVo();
        roleDateVo.setTotal((int) count);
        Integer pages = Math.toIntExact(count / limit);
        if (count % limit > 0) {
            pages++;
        }
        roleDateVo.setPages(pages);
        roleDateVo.setLimit(limit);
        roleDateVo.setPage(page);
        roleDateVo.setList(roleListVos);
        return roleDateVo;

    }

    @Override
    public CreateVo create(CreateBo createBo) {
        Integer count=marketRoleMapper.selectByName(createBo.getName());
        if (count!=0){
            return null;
        }
        MarketRole marketRole = new MarketRole();
        marketRole.setName(createBo.getName());
        marketRole.setDesc(createBo.getDesc());
        marketRole.setAddTime(new Timestamp(System.currentTimeMillis()));
        marketRole.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        int i = marketRoleMapper.insertSelective(marketRole);
        CreateVo createVo = new CreateVo();
        createVo.setId(marketRole.getId());
        createVo.setName(marketRole.getName());
        createVo.setDesc(marketRole.getDesc());
        createVo.setAddTime(marketRole.getAddTime().toString());
        createVo.setUpdateTime(marketRole.getUpdateTime().toString());
        return createVo;
    }

    @Override
    public Integer update(MarketRole marketRole) {
        marketRole.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        Integer count=marketRoleMapper.selectRoleData(marketRole.getName());
        String name = marketRoleMapper.selectNameById(marketRole.getId());
        if (!marketRole.getName().equals(name)){
            return 1;
        }
        marketRoleMapper.update(marketRole);
        return 0;
    }

    @Override
    public void delete(MarketRole marketRole) {
        marketRole.setDeleted(true);
        marketRoleMapper.updateByPrimaryKeySelective(marketRole);

    }

    @Override
    public OptionsVo options() {
        LinkedList<RoleVo> roleVos = marketRoleMapper.selectAllRole();
        Integer count = marketRoleMapper.countByExample(null);
        OptionsVo optionsVo = new OptionsVo();
        optionsVo.setPages(1);
        optionsVo.setTotal(count);
        optionsVo.setLimit(count);
        optionsVo.setList(roleVos);
        optionsVo.setPage(1);
        return optionsVo;
    }


    /**
     * 显示回显授权数据
     * 进行对角色的授权管理
     * @param roleId
     * @return
     */
    @Override
    public DataDTO permissions(Integer roleId) {

        //查询权限并回显
        LinkedList<String> assignedPermissions = marketRoleMapper.selectPermissionByRoleId(roleId);
        LinkedList<SystemPermissionsDTO> systemPermissionsDTO=marketRoleMapper.selectAllPermission();
        if ("*".equals(assignedPermissions.pollFirst())){
            for (SystemPermissionsDTO permissionsDTO : systemPermissionsDTO) {
                for (ChildrenDTOX child : permissionsDTO.getChildren()) {
                    assignedPermissions.add(child.getId());
                }
            }
        }
        DataDTO dataDTO = new DataDTO();
        dataDTO.setAssignedPermissions(assignedPermissions);
        dataDTO.setSystemPermissions(systemPermissionsDTO);
        return dataDTO;
    }


    @Override
    public DataDTO permissions2(PermissionsBo permissions) {
        // 全部删除，逻辑删除
        marketRoleMapper.deleteRolePermisssionnByRoleId(permissions.getRoleId());
        //然后增加数据
        MarketPermission marketPermission = new MarketPermission();
        marketPermission.setRoleId(permissions.getRoleId());
        marketPermission.setAddTime(new Timestamp(System.currentTimeMillis()));
        marketPermission.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        for (String permission : permissions.getPermissions()) {
            marketPermission.setPermission(permission);
            marketPermission.setDeleted(false);
            marketRoleMapper.insertRolePermisssionnByRoleId(marketPermission);
        }
        DataDTO dataDTO = permissions(permissions.getRoleId());
        return dataDTO;
    }

}
